package jpm.common;

import jpm.common.JPMException;
import jpm.common.JPMException.ErrorCode;


public class AssertUtils {
	public static void ensure(boolean condition) {
		ensure(condition, "Assert failure");
	}

	public static void ensure(boolean condition, String messageIfError) {

		if (condition)
			return;

		// failed condition
		if (null == messageIfError)
			throw new JPMException().errorCode(ErrorCode.ERROR_INVALID_INPUT)
					.message("messageIfError is null");

		throw new JPMException().errorCode(ErrorCode.ERROR_ASSERT_FAIL)
				.message(messageIfError + " (AssertUtils.ensure failed)");

	}

	public static void ensureNotNull(Object obj, String messageIfError) {

		if (obj != null)
			return;

		// failed condition
		if (null == messageIfError)
			throw new JPMException().errorCode(ErrorCode.ERROR_INVALID_INPUT)
					.message("messageIfError is null");

		throw new JPMException()
				.errorCode(ErrorCode.ERROR_ASSERT_FAIL)
				.message(messageIfError + " (AssertUtils.ensureNotNull failed)");

	}

	public static void ensureNotNull(Object obj) {
		ensureNotNull(obj, "Assert failure");
	}

	public static void ensureEqual(int expected, int actual,
			String messageIfError) {

		if (expected == actual)
			return;

		// failed condition
		String errMessage = "Expected [" + expected + "] actual [" + actual
				+ "] (AssertUtils.ensureEqual failed)";

		if (messageIfError == null)
			throw new JPMException().errorCode(ErrorCode.ERROR_INVALID_INPUT)
					.message("messageIfError is null");

		errMessage = messageIfError + ". " + errMessage;

		throw new JPMException().errorCode(ErrorCode.ERROR_ASSERT_FAIL)
				.message(errMessage);
	}

	public static void ensureEqual(int expected, int actual) {
		ensureEqual(expected, actual, "Assert failure");
	}
}